{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "transfer done\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "import numpy as np\n",
    "\n",
    "fA = open('A.csv', 'r')\n",
    "fB = open('B.csv', 'r')\n",
    "wf = open('B_fix.csv', 'w')\n",
    "\n",
    "A_data = list(csv.reader(fA))\n",
    "B_data = list(csv.reader(fB))\n",
    "rows = len(A_data)\n",
    "# print(A_data[:][0])\n",
    "mod_flag = False\n",
    "del_tone = 0\n",
    "PBS = 2\n",
    "cent_per_tone = 8192/PBS\n",
    "\n",
    "for t in range(0,rows):\n",
    "    if 'Note_on' in A_data[t][2]:\n",
    "        if A_data[t][4]!=B_data[t][4]:\n",
    "            del_tone = int(B_data[t][4])-int(A_data[t][4])\n",
    "            mod_flag=True\n",
    "            shift_cent = int(cent_per_tone*del_tone)\n",
    "    elif 'Note_off' in A_data[t][2]:\n",
    "        mod_flag=False\n",
    "    elif mod_flag:\n",
    "        B_data[t][4] = str(np.clip(int(B_data[t][4])+shift_cent ,0,16383))\n",
    "    wf.write(','.join(B_data[t]))\n",
    "    wf.write('\\n')\n",
    "print(\"transfer done\")\n",
    "fA.close()\n",
    "fB.close()\n",
    "wf.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "A2\n"
     ]
    }
   ],
   "source": [
    "from os import listdir\n",
    "from os.path import isfile, join\n",
    "import os\n",
    "os.system(\"mkdir .\\A\\Acsv\")\n",
    "os.system(\"mkdir .\\B\\Bcsv\")\n",
    "\n",
    "A_dir = 'A'\n",
    "all_files = [f for f in listdir(A_dir) if isfile(join(A_dir, f))]\n",
    "for file in all_files:\n",
    "    if file.find('.mid') != -1:\n",
    "        song_name = file.replace('.mid','')\n",
    "        csv_name = song_name+'csv'\n",
    "        print(song_name)\n",
    "        os.system(\"Csvmidi .\\\"+A_dir+\"\"+file)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Song1\n",
      "Song2\n"
     ]
    }
   ],
   "source": [
    "all_song_dir = 'All_song'\n",
    "A_name = 'A'\n",
    "B_name = 'B'\n",
    "\n",
    "for f in listdir(all_song_dir):\n",
    "    song_path = str('.\\'+all_song_dir+'\\'+f+'\\')\n",
    "    for file in song_path :\n",
    "        song_name = file.replace('.mid','')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
